package com.hissy.cn.controller;

import com.alibaba.fastjson.JSON;
import com.hissy.cn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.Date;

/**
 * Created by zhangyanfei on 2019/1/22.
 * com.hissy.cn.controller.DemoController
 * @author zhangyanfei
 * @date 2019/01/22
 *
 * 服务注册与发现
 *
 * 服务网关
 *
 * 链路监控 pinpoint
 *
 * 分布式配置中心
 *
 * 分布式任务调度中心
 *
 * 消息中间件服务 mns kafka
 */
@Controller
public class DemoController {
    @Autowired
    private UserService userService;


    @RequestMapping("/index")
    public ModelAndView getUserList(ModelAndView mv){
        mv.addObject("users",userService.getUserInfoList());
        mv.setViewName("index");
        return mv;
    }

    @RequestMapping("")
    public ModelAndView toIndex(ModelAndView mv){
        mv.addObject("users",userService.getUserInfoList());
        mv.setViewName("index");
        return mv;
    }

    @GetMapping("/json")
    public User getUser(){
        return new User("zhangyanfei", 10, new Date());
    }

    /**
     * @RequestBody
     *
     * json字符串
     * @RequestBody  -> application/json（json  会触发jackson的功能）
     *
     * 不加参数-> 自动绑定对象
     *
     * @param user
     */
    @PostMapping("/setUser")
    public String setUser(@RequestBody User user){
        return JSON.toJSONString(user);

    }

}
